#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QTime>
#include <QDebug>
#include <qdatetime.h>

#include <iostream>
#include <fstream>

using namespace std;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

#include <asm/types.h>
#include <linux/videodev2.h>

#include <opencv2/opencv.hpp>
#include <QString>


#include "/home/ysy/QT/VDMA.c"
#include "/home/ysy/QT/xcvt_resize.c"
#include "/home/ysy/QT/xcvt_resize_linux.c"
#include "/home/ysy/QT/interpret_output.cpp"

/*
#include "/home/yu/QTworkspace/interpret_output.cpp"
#include "/home/yu/QTworkspace/interpret_output.h"
*/

#include <QPainter>

#include "global.h"

#define CAMERA_DEVICE "/dev/video0"

#define VIDEO_WIDTH 640
#define VIDEO_HEIGHT 480
#define VIDEO_FORMAT V4L2_PIX_FMT_YUYV
#define BUFFER_COUNT 4
#define VDMA_READ 1
#define VDMA_WRITE 0

#define lOCK_READY 0
#define lOCK_DONE 1

#define output_length 25088//24500

#define USB_ALERT 0

int readstate;
int debug_main = 1;
char * buffer[12] = {0};
char file_name[100];
FILE * pFile;
long lSize;
size_t result;
CvCapture *pCapture = NULL;
IplImage *pFrame = NULL, *srcImage=NULL;
IplImage *img_out_1 =cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
IplImage *img_out_2 =cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,4);
IplImage *img_out_3 =cvCreateImage(cvSize(960,720),IPL_DEPTH_8U,4);
QImage *image;

void camera_setup()
{

    qDebug() << "open camera";


    // try to input test img -----------------------
    //testfd = open("/home/linaro/testimg",O_RDWR);
    if(1){
        test_img_id = 0;
        /* 若要一个byte不漏地读入整个文件，只能采用二进制方式打开 */
        for(int i = 0;i<2;i++){
            sprintf(file_name, "/home/linaro/res/testimg%d", i);
            qDebug() << "read img "<< file_name;
            pFile = fopen (file_name, "rb" );
            fseek (pFile , 0 , SEEK_END);
            lSize = ftell (pFile);
            rewind (pFile);
            buffer[i] = (char*) malloc (sizeof(char)*lSize*4);
            result = fread (buffer[i],1,lSize,pFile);
            fclose (pFile);
            usleep(100000);
        }
        testfd = (int)buffer[0];
    }

    if(USE_CAMERA)fd = open(CAMERA_DEVICE, O_RDWR, 0);
    // ---------------------------------------------
    // 获取驱动信息
    struct v4l2_capability cap;
    if(USE_CAMERA)ioctl(fd, VIDIOC_QUERYCAP, &cap);

    // 设置视频格式
    struct v4l2_format fmt;
    memset(&fmt, 0, sizeof(fmt));
    fmt.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    fmt.fmt.pix.width       = VIDEO_WIDTH;
    fmt.fmt.pix.height      = VIDEO_HEIGHT;
    fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
    fmt.fmt.pix.field       = V4L2_FIELD_INTERLACED;
    if(USE_CAMERA)ioctl(fd, VIDIOC_S_FMT, &fmt);

    // 获取视频格式
    if(USE_CAMERA)ioctl(fd, VIDIOC_G_FMT, &fmt);


    // 请求分配内存



    // 获取空间

    qDebug() << "framecontrol";

    framecontrol = (struct VideoControl*) calloc( 1, sizeof(*framecontrol) );

    framecontrol[0].vdmaHandler = open("/dev/mem", O_RDWR | O_SYNC);

    framecontrol[0].start = (unsigned int*) mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, framecontrol[0].vdmaHandler, (off_t)((unsigned int)0x30000000));
    memset(framecontrol[0].start,0,4096);
    usleep(1000);


    framecontrol[0].NUM = BUFFER_COUNT;
    framecontrol[0].PERIOD = 25;
    framecontrol[0].lOCK_STATE = lOCK_READY;
    framecontrol[0].TX_ID = 0;
    framecontrol[0].WK_ID = 0;
    framecontrol[0].RX_ID = 0;
    framecontrol[0].ADDR_T0 = (unsigned int)(0x3A000000);
    framecontrol[0].ADDR_T1 = (unsigned int)(0x3B000000);
    framecontrol[0].ADDR_T2 = (unsigned int)(0x3C000000);
    framecontrol[0].ADDR_T3 = (unsigned int)(0x3D000000);
    framecontrol[0].ADDR_R0 = (unsigned int)(0x3E000000);
    framecontrol[0].ADDR_R1 = (unsigned int)(0x3E800000);
    framecontrol[0].ADDR_R2 = (unsigned int)(0x3F000000);
    framecontrol[0].ADDR_R3 = (unsigned int)(0x3F800000);
    framecontrol[0].MREADY = 0;
    framecontrol[0].SREADY = 0;
    framecontrol[0].STATED = 0;
    framecontrol[0].ATT = 0;
    framecontrol[0].LAYER = 0;
    framecontrol[0].NUMD = 0;
    framecontrol[0].PIECEEND = 0;
    framecontrol[0].WP = 0;

    (framecontrol[0].start)[1] = framecontrol[0].NUM;
    (framecontrol[0].start)[2] = framecontrol[0].PERIOD;
    (framecontrol[0].start)[3] = framecontrol[0].lOCK_STATE;
    (framecontrol[0].start)[4] = framecontrol[0].TX_ID;
    (framecontrol[0].start)[5] = framecontrol[0].WK_ID;
    (framecontrol[0].start)[6] = framecontrol[0].RX_ID;
    (framecontrol[0].start)[7] = framecontrol[0].ADDR_T0;
    (framecontrol[0].start)[8] = framecontrol[0].ADDR_T1;
    (framecontrol[0].start)[9] = framecontrol[0].ADDR_T2;
    (framecontrol[0].start)[10] = framecontrol[0].ADDR_T3;
    (framecontrol[0].start)[11] = framecontrol[0].ADDR_R0;
    (framecontrol[0].start)[12] = framecontrol[0].ADDR_R1;
    (framecontrol[0].start)[13] = framecontrol[0].ADDR_R2;
    (framecontrol[0].start)[14] = framecontrol[0].ADDR_R3;
    (framecontrol[0].start)[15] = framecontrol[0].MREADY;
    (framecontrol[0].start)[16] = framecontrol[0].SREADY;
    (framecontrol[0].start)[17] = framecontrol[0].STATED;
    (framecontrol[0].start)[18] = framecontrol[0].ATT;
    (framecontrol[0].start)[19] = framecontrol[0].LAYER;
    (framecontrol[0].start)[20] = framecontrol[0].NUMD;
    (framecontrol[0].start)[21] = framecontrol[0].PIECEEND;
    (framecontrol[0].start)[22] = framecontrol[0].WP;


    reqbuf.count = BUFFER_COUNT;
    reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    reqbuf.memory = V4L2_MEMORY_MMAP;

    if(USE_CAMERA)ioctl(fd , VIDIOC_REQBUFS, &reqbuf);


    qDebug() << "framebuf";
    framebuf = (struct VideoBuffer*) calloc( reqbuf.count, sizeof(*framebuf) );


    unsigned int i;
    for (i = 0; i < reqbuf.count; i++)
    {
        buf.index = i;
        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        buf.memory = V4L2_MEMORY_MMAP;

        if(USE_CAMERA)ioctl(fd , VIDIOC_QUERYBUF, &buf);

        // mmap buffer
        framebuf[i].length = buf.length;
        if(debug_main){
            framebuf[i].start = (unsigned int*) testfd;
        }else{
            framebuf[i].start = (unsigned int*) mmap(NULL, buf.length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, buf.m.offset);
        }


        // change fd to testfd to input testimg
        qDebug() << "mmapc.."<<(unsigned int)buf.m.offset;
        qDebug() << "framebuf.length: "<< buf.length;
        // Queen buffer

        if(USE_CAMERA)ioctl(fd , VIDIOC_QBUF, &buf);


    }

    qDebug() << "fbaddr";

    fbaddr0[0].fbP = (unsigned int)0x32000000;
    fbaddr0[1].fbP = (unsigned int)0x33000000;
    fbaddr0[2].fbP = (unsigned int)0x34000000;
    fbaddr0[3].fbP = (unsigned int)0x35000000;

    fbaddr1[0].fbP = (unsigned int)0x36000000;
    fbaddr1[1].fbP = (unsigned int)0x37000000;
    fbaddr1[2].fbP = (unsigned int)0x38000000;
    fbaddr1[3].fbP = (unsigned int)0x39000000;

    fbaddr2[0].fbP = (unsigned int)0x3A000000;
    fbaddr2[1].fbP = (unsigned int)0x3B000000;
    fbaddr2[2].fbP = (unsigned int)0x3C000000;
    fbaddr2[3].fbP = (unsigned int)0x3D000000;

    fbaddr3[0].fbP = (unsigned int)0x3E000000;
    fbaddr3[1].fbP = (unsigned int)0x3E800000;
    fbaddr3[2].fbP = (unsigned int)0x3F000000;
    fbaddr3[3].fbP = (unsigned int)0x3F800000;


    readtemp[0].fbP = (unsigned int)0x31000000;
    readtemp[1].fbP = (unsigned int)0;

    readtemp[0].fbV = (unsigned char*) mmap(NULL, 1024*1024*2*sizeof(char), PROT_READ|PROT_WRITE, MAP_SHARED, framecontrol[0].vdmaHandler, (off_t)readtemp[0].fbP);
    readtemp[1].fbV = (unsigned char*) malloc(1024*1024*2*sizeof(char));

    /*back_pv = (short*)mmap(NULL,output_length*2*sizeof(char),PROT_READ|PROT_WRITE, MAP_SHARED, framecontrol[0].vdmaHandler, (off_t)((unsigned int)0x30200000));

    for(int kk=0;kk<output_length;kk++){
        back_pv[kk] = back[kk];
    }*/

    memset(readtemp[0].fbV, 0, 1024*1024*2*sizeof(char));
    memset(readtemp[1].fbV, 0, 1024*1024*2*sizeof(char));

    qDebug() << "vdma_setup";
    vdma_setup(	&vdma0, (unsigned int)0x43010000, 640, 480, 2, fbaddr0);
    vdma_setup(	&vdma1, (unsigned int)0x43020000, 960, 720, 4, fbaddr1);
    vdma_setup(	&vdma2, (unsigned int)0x43030000, 448, 448, 8, fbaddr2);

    qDebug() << "mmap_output_length";
    for(int i=0;i<reqbuf.count;i++){
        fbaddr3[i].fbV = (unsigned int*)mmap(NULL, output_length*2*sizeof(char), PROT_READ | PROT_WRITE, MAP_SHARED, framecontrol[0].vdmaHandler, (off_t)fbaddr3[i].fbP);
        qDebug() << "mmap1.."<< fbaddr3[i].fbV;
        memset(fbaddr3[i].fbV, 0, output_length*2*sizeof(char));
    }




    qDebug() << "XCvt_resize_Initialize";
    //    IplImage *pFrame = NULL, *srcImage=NULL;
    //    CvCapture *pCapture = NULL;
    //    pCapture =cvCreateFileCapture("rtsp://admin:y1997212@172.16.6.42/Streaming/Channels/1");
    //    double rate=cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS);

    //    CvSize size=cvSize((int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_WIDTH),(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_HEIGHT));

    //    CvVideoWriter*writer=cvCreateVideoWriter("videotest.avi",CV_FOURCC('M','J','P','G'),rate,size);

    //    pFrame = cvQueryFrame(pCapture);

    //    srcImage=cvCloneImage(pFrame);
    //    cvWriteFrame(writer,srcImage);
    //    cvShowImage("123234",srcImage);
        //cout<<pFrame->width<<","<<pFrame->height<<endl;

    //    cvWaitKey(30);

    //    cvReleaseImage(&srcImage);

    //    srcImage=NULL;
    if(XST_SUCCESS != XCvt_resize_Initialize(&Cvt_resize, "my_uio"))
     {
         printf("XCvt_resize_Initialize failed\n");
     }

    XCvt_resize_Set_rows(&Cvt_resize, 480);//input
    XCvt_resize_Set_cols(&Cvt_resize, 640);

    XCvt_resize_Set_rows2(&Cvt_resize, 720);//display
    XCvt_resize_Set_cols2(&Cvt_resize, 960);

    XCvt_resize_Set_rows3(&Cvt_resize, 448);//chip
    XCvt_resize_Set_cols3(&Cvt_resize, 448);

    XCvt_resize_DisableAutoRestart(&Cvt_resize);
    XCvt_resize_InterruptClear(&Cvt_resize,0xf);


    qDebug() << "XCvt_resize_Get_cols" << XCvt_resize_Get_cols(&Cvt_resize);



    qDebug() << "vdma_start_buffering_set";
    vdma_start_buffering_set(&vdma0, VDMA_WRITE);
    qDebug() << "vdma_start_0";
    vdma_start_buffering_set(&vdma1, VDMA_READ);
    qDebug() << "vdma_start_1";
    vdma_start_buffering_set(&vdma2, VDMA_READ);
    qDebug() << "vdma_start_2";


    vdma_mm2s_status_dump(&vdma0);
    vdma_s2mm_status_dump(&vdma1);

    // 开始录制
    enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

    if(USE_CAMERA)ioctl(fd, VIDIOC_STREAMON, &type);


    //frame process & display
    //iplImage = cvCreateImageHeader(cvSize(VIDEO_WIDTH,VIDEO_HEIGHT),8,2);
    iplImage_resize = cvCreateImage(cvSize(448,448),16,4);
    for(int kk=0;kk<BUFFER_COUNT;kk++){
        cv::Mat dst(720, 960, CV_8UC3);
//        IM_show[kk] = cvCreateImageHeader(cvSize(960,720),8,4);
        IM_show[kk] = dst;
    }
    tx_id = 0;


}

const char *devusb  = "/dev/ttyUSB0";
uint8_t alert_start[9];
uint8_t alert_stop[9];
serialPort myserial;
void MainWindow::init_serial(){
    cout<<"serialPort Init"<<endl;
    myserial.OpenPort(devusb);
    myserial.setup(9600,0,8,1,'N');
    alert_start[0] = 0x01;
    alert_start[1] = 0x10;
    alert_start[2] = 0x00;
    alert_start[3] = 0x1A;
    alert_start[4] = 0x00;
    alert_start[5] = 0x01;
    alert_start[6] = 0x01;
    alert_start[7] = 0xCE;
    alert_start[8] = 0x18;

    alert_stop[0] = 0x01;
    alert_stop[1] = 0x10;
    alert_stop[2] = 0x00;
    alert_stop[3] = 0x1A;
    alert_stop[4] = 0x00;
    alert_stop[5] = 0x01;
    alert_stop[6] = 0x00;
    alert_stop[7] = 0x0F;
    alert_stop[8] = 0xD8;
    cout<<"serialPort Init end"<<endl;
}

void MainWindow::test_serial()
{
    cout<<"serialPort write"<<endl;
    int write_num = 0;
    while (true)
    {
      write_num = myserial.writeBuffer(alert_start, 9);
      cout<<"serialPort write"<< write_num << endl;
      usleep(1000000);
      write_num = myserial.writeBuffer(alert_stop, 9);
      cout<<"serialPort write"<< write_num << endl;
      usleep(1000000);
      if(QString("alert")==QString("alert")){
          cout<<"alert==alert" << endl;
      }
    }
}

void MainWindow::start_alert(){
    if(USB_ALERT){
        cout<<"start_alert"<<endl;
        myserial.writeBuffer(alert_start, 9);
    }
}

void MainWindow::stop_alert(){
    if(USB_ALERT){
        cout<<"stop_alert"<<endl;
        myserial.writeBuffer(alert_stop, 9);
    }
}



void camera_release()
{
        int i;
        cvReleaseImage(&iplImage_resize);
        //cvReleaseImageHeader(&iplImage);
        for(int kk=0;kk<BUFFER_COUNT;kk++){
//            cvReleaseImageHeader(&IM_show[kk]);
        }
        for (i=0; i< reqbuf.count; i++)
        {
                munmap(framebuf[i].start, framebuf[i].length);
        }
        if(USE_CAMERA)close(fd);
        close(testfd);

        munmap(framecontrol[0].start, 4096);

        munmap(readtemp[0].fbV,2*1024*1024*sizeof(char));

        //munmap(back_pv,output_length*2*sizeof(char));

        free(readtemp[0].fbV);
        close(framecontrol[0].vdmaHandler);

        vdma_halt(&vdma0, fbaddr0);
        vdma_halt(&vdma1, fbaddr1);
        vdma_halt(&vdma2, fbaddr2);
        XCvt_resize_Release(&Cvt_resize);
}


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
//    int test_int = 1;
//    qDebug() << "int size" << sizeof(test_int);
//    exit(0);

    ui->setupUi(this);
    timer = new QTimer(this);
    timer_slow = new QTimer(this);
    //初始化timer
    qDebug() << "MainWindow";
    //cam = NULL;
    // 初始化cam

    this->setWindowFlags(Qt::FramelessWindowHint);

    ui->label_3->setText("Initializing camera");
    ui->label_3->show();
    camera_setup();
    readstate = 0;
    framecontrol[0].SREADY = 1;
    (framecontrol[0].start)[16] = framecontrol[0].SREADY;

    ui->label_3->setText("Initializing C2C");
    ui->label_3->show();

    if(debug_main){
        QObject::connect(timer_slow, SIGNAL(timeout()), this, SLOT(cameraopen_O()));
    }
    else{
        QObject::connect(timer, SIGNAL(timeout()), this, SLOT(cameraopen_O()));
    }
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(cameraopen_D()));
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(cameraopen()));
    QObject::connect(&thread, SIGNAL(send(QString)), this, SLOT(accept(QString)));
    QObject::connect(&thread_disp, SIGNAL(sendN(int)), this, SLOT(display(int)));
    QObject::connect(&thread_disp, SIGNAL(send(QString)), this, SLOT(accept(QString)));
//    QObject::connect(this, SIGNAL(s_display_img(QImage)), this, SLOT(dispaly_img(QImage)));

    // 时间到，读取当前摄像头信息,这个必须加，因为timer需要配合timeout()信号用
    //没有的话会出现错误；

    qDebug() << "MainWindow label1";
//    QPixmap("/home/linaro/cass.jpg");
    qDebug() << "MainWindow label4";
//    ui->label_2->setPixmap(QPixmap("/home/linaro/cass.jpg"));
//    ui->label_2->show();

    qDebug() << "MainWindow label2";
    ui->label_3->setText("Start transfer");
    ui->label_3->show();
    qDebug() << "MainWindow label3";
    timer->start(25);
    timer_slow->start(25);
    if(USB_ALERT){
        init_serial();
    }
    //test_serial();
    qDebug() << "MainWindow End";
    // 开始计时，超时则发出timeout()信号

    if(0){
        qDebug() << "test rtsp";
        IplImage *pFrame = NULL, *srcImage=NULL;
        pCapture =cvCreateFileCapture("rtsp://admin:y1997212@172.16.6.42/Streaming/Channels/1");
        pFrame = cvQueryFrame(pCapture);
        qDebug() << "test rtsp cvQueryFrame";
        srcImage=cvCloneImage(pFrame);
        qDebug() << "test rtsp cvCloneImage";
        qDebug() << "test rtsp display";
        QImage *image;
        cvCvtColor(srcImage,srcImage,CV_BGR2RGB);
        uchar *imgData=(uchar *)srcImage->imageData;
        image=new QImage(imgData,srcImage->width,srcImage->height,QImage::Format_RGB888);
        ui->label->setPixmap(QPixmap::fromImage(*image));
        ui->label->show();
        qDebug() <<pFrame->width<<","<<pFrame->height<<endl;
        cvReleaseImage(&srcImage);
        qDebug() << "test rtsp cvReleaseImage";
        srcImage=NULL;
    }
    if(0){
        qDebug() << "c++ rtsp";
        cv::Mat frame_main;
        cv::VideoCapture cap_main("rtsp://admin:y1997212@172.16.6.42/Streaming/Channels/1");
        cap_main>>frame_main;
        QImage img =QImage(frame_main.data, frame_main.cols, frame_main.rows, frame_main.step, QImage::Format_RGB888);
        ui->label->setPixmap(QPixmap::fromImage(img));
        ui->label->show();
        qDebug() << "c++ rtsp display end";
    }
}



MainWindow::~MainWindow()
{

    timer->stop();
    // 停止读取数据。

     camera_release();
    //释放内存；

    delete ui;
}

QString alert_flag = QString("alert");
int msg_num = 0;
void MainWindow::accept(QString msg)
{
    ui->label_3->setText(msg);
    ui->label_3->show();
    qDebug()<<"accept"<<msg;
    if(msg_num%10==0){
        if(msg==alert_flag){
            start_alert();
        }else{
            stop_alert();
        }
    }
    msg_num += 1;
    msg_num = msg_num % (50*TEST_IMG_NUM);
    if(msg_num%50==0){
        qDebug()<<"change testfd";
        changeDisplay();
    }
}

int load_img = 0;
int use_buffer_id = 0;
void MainWindow::changeDisplay(){

    return;
    testfd = (int)buffer[use_buffer_id];
    for(int i=0;i<BUFFER_COUNT;i++){
        framebuf[i].start = (unsigned int*)testfd;
    }

    load_img = load_img + 1;
    use_buffer_id = (use_buffer_id+1)%2;

    if(0){
        sprintf(file_name, "/home/linaro/res/testimg%d", load_img);
        qDebug() << "read img "<< file_name;
        pFile = fopen (file_name, "rb" );
        if(!pFile){
            load_img = 0;
            sprintf(file_name, "/home/linaro/res/testimg%d", load_img);
            qDebug() << "read img "<< file_name;
            pFile = fopen (file_name, "rb" );
        }
        fseek (pFile , 0 , SEEK_END);
        lSize = ftell (pFile);
        rewind (pFile);
    //    buffer[use_buffer_id] = (char*) malloc (sizeof(char)*lSize*4);
        result = fread (buffer[use_buffer_id],1,lSize,pFile);
        fclose (pFile);
    }
    else{
        IplImage *pFrame = NULL, *srcImage=NULL;
        IplImage *img_out_1 =cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
        IplImage *img_out_2 =cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,4);
        pCapture =cvCreateFileCapture("rtsp://admin:y1997212@172.16.6.42/Streaming/Channels/1");
        pFrame = cvQueryFrame(pCapture);
        qDebug() << "test rtsp cvQueryFrame";
        srcImage=cvCloneImage(pFrame);
        qDebug() << "test rtsp cvCloneImage";
        qDebug() << "test rtsp display";
        cvResize(srcImage,img_out_1);
        cvCvtColor(img_out_1,img_out_2,CV_BGR2RGBA);
        uchar *imgData=(uchar *)srcImage->imageData;
        qDebug() <<pFrame->width<<","<<pFrame->height<<endl;
        memcpy(fbaddr2[tx_id].fbV, imgData,802816*sizeof(short));
        msync((fbaddr2[tx_id].fbV), 802816*sizeof(short), MS_SYNC);
        cvReleaseImage(&srcImage);
        qDebug() << "test rtsp cvReleaseImage";
        srcImage=NULL;
    }
}

void MainWindow::display(int shownum)
{
    //qDebug()<<"MainWindow::display";
    ui->label->setPixmap(QPixmap::fromImage(qqimage[shownum]));
    ui->label->show();
}

void MainWindow::display_img(QImage img){
    qDebug() << "test rtsp display_img";
    ui->label->setPixmap(QPixmap::fromImage(img));
    ui->label->show();
}

void MainWindow::cameraopen_O()
{
    thread_open.start();
    return;

    try{
    pFrame = cvQueryFrame(pCapture);
    qDebug() << "test rtsp cvQueryFrame";
    srcImage=cvCloneImage(pFrame);
    qDebug() << "test rtsp cvCloneImage";
    cvResize(srcImage,img_out_1);
    qDebug() << "test rtsp cvResize";
    cvCvtColor(img_out_1,img_out_2,CV_BGR2RGBA);
    cvResize(img_out_2,img_out_3);



    cvCvtColor(srcImage,srcImage,CV_BGR2RGB);
    uchar *imgData=(uchar *)srcImage->imageData;
    image=new QImage(imgData,srcImage->width,srcImage->height,QImage::Format_RGB888);
    emit s_display_img(*image);
    qDebug() << "test rtsp cvCvtColor";
    qDebug() <<pFrame->width<<","<<pFrame->height<<endl;
    memcpy(fbaddr2[tx_id].fbV, imgData,802816*sizeof(short));
    msync((fbaddr2[tx_id].fbV), 802816*sizeof(short), MS_SYNC); // for cass compute
    cvReleaseImage(&srcImage);

    qDebug() << "test rtsp cvReleaseImage";
    srcImage=NULL;}
    catch (...) {
        qDebug() << "test rtsp read error";
        try {
            cvReleaseCapture(&pCapture);
        } catch (...) {
            qDebug() << "to_pCapture release error";
        }
        pCapture =cvCreateFileCapture("rtsp://admin:y1997212@172.16.6.42/Streaming/Channels/1");
    }
}

void MainWindow::cameraopen_D()
{
//        qDebug() << "test rtsp cameraopen_D";
    thread_disp.start();
}

void MainWindow::cameraopen()
{


    if(readstate==0){

        readstate=1;

        thread.start();

        ui->label_3->setText("The CASSANN-X is working");
        ui->label_3->show();
    }

}


